<!DOCTYPE html>
<html lang="en">
    <include file="Public:header" />
        <include file="Public:page_css" />
        <link rel="stylesheet" href="__PUBLIC__/Admin/css/checkbox.css" />
            <body class="fixed-sidebar full-height-layout gray-bg" style="overflow:hidden">
                <include file="Public:nav" />
                    <div class="row wrapper border-bottom white-bg page-heading">
                        <div class="col-lg-12">
                            <h2>
                                数据库备份
                            </h2>
                            <ol class="breadcrumb">
                                <li><a href="{:U('Index/index')}"><i class="fa fa-home"></i>主页</a></li>
                                <li><a>数据库管理</a></li>
                                <li><strong>数据库备份</strong></li>
                            </ol>
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-xs-12 col-sm-8">
                            <button class="btn btn-primary">
                                <i class="ace-icon fa fa-database bigger-110">
                                </i>
                                <a id="export" href="javascript:;" style="color:#FFFFFF" autocomplete="off">
                                    备份当前数据
                                </a>
                            </button>
                            <a id="optimize" href="{:U('optimize')}">
                                <button class="btn btn-primary">
                                    <i class="ace-icon fa fa-check-circle bigger-110">
                                    </i>
                                    优化数据表
                                </button>
                            </a>
                            <a id="repair"  href="{:U('repair')}">
                                <button class="btn btn-primary">
                                    <i class="ace-icon fa fa-check-square-o bigger-110">
                                    </i>
                                    修复数据表
                                </button>
                            </a>
                        </div>
                    </div><br/>
                    <div class="row">
                        <div class="col-xs-12">
                            <div>
                                <form id="export-form" method="post" class="form-horizontal"  action="{:U('export')}">
                                    <table width="100%" class="table table-striped table-bordered table-hover" id="dynamic-table">
                                        <thead>
                                            <tr>
                                                <th width="4%" align="center" style="text-align:center;">
                                                    <label class="pos-rel">
                                                        <input class="check-all ace ace-checkbox-2" id='chkAll' checked="chedked" type="checkbox" value=""  onclick='CheckAll(this.form)'/>
                                                        <span class="lbl">
                                                        </span>
                                                    </label>
                                                </th>
                                                <th width="20%">表名</th>
                                                <th width="13%">数据量</th>
                                                <th width="18%">数据大小</th>
                                                <th width="22%">创建时间</th>
                                                <th width="20%" style="border-right:#CCC solid 1px;">
                                                    操作
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <foreach name="data_list" item="v">
                                                <tr>
                                                    <td height="28" align="center" >
                                                        <label class="pos-rel">
                                                            <input class="ids ace ace-checkbox-2" checked="chedked" type="checkbox" name="tables[]" value="{$v.name}"/>
                                                            <span class="lbl">
                                                            </span>
                                                        </label>
                                                    </td>
                                                    <td>{$v.name}</td>
                                                    <td>【{$v.rows}】 条记录</td>
                                                    <td>{$v.data_length|format_bytes}</td>
                                                    <td>{$v.create_time}</td>
                                                    <td>
                                                        <div class="btn btn-primary btn-circle">
                                                            <a class="green" href="{:U('optimize?tables='.$v['name'])}" title="优化表">
                                                                <i class="fa fa-list">
                                                                </i>
                                                            </a>
                                                        </div>
                                                        &nbsp;
                                                        <div class="btn btn-info btn-circle">
                                                            <a class="info" href="{:U('repair?tables='.$v['name'])}" title="修复表">
                                                                <i class="fa fa-check">
                                                                </i>
                                                            </a>
                                                        </div>
                                                    </td>
                                                </tr>
                                            </foreach>
                                            <tr>
                                                <td height="50" colspan="7" align="left">
                                                    &nbsp;
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /.page-content -->
            </div>
        </div>
        <!-- /.main-content -->
    <script type="text/javascript">
    

    function unselectall(){
    if(document.myform.chkAll.checked){
        document.myform.chkAll.checked = document.myform.chkAll.checked&0;
        }
    }


    function CheckAll(form){
        for (var i=0;i<form.elements.length;i++){
            var e = form.elements[i];
            if (e.Name != 'chkAll'&&e.disabled==false)
            e.checked = form.chkAll.checked;
        }
    }


    
    (function($){
        var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

        $optimize.add($repair).click(function(){
            $.post(this.href, $form.serialize(), function(data){
                if(data.status){
            layer.alert(data.info, {icon: 6}, function(index){
            layer.close(index);
            window.location.href=data.url;
            });
                } else {
            layer.alert(data.info, {icon: 6}, function(index){
            layer.close(index);
            window.location.href=data.url;
            });
                }
                setTimeout(function(){
                    $('#top-alert').find('button').click();
                    $(that).removeClass('disabled').prop('disabled',false);
                },1500);
            }, "json");
            return false;
        });

        $export.click(function(){
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                $form.attr("action"),
                $form.serialize(),
                function(data){
                    if(data.status){
                        tables = data.tables;
                        $export.html(data.info + "开始备份，请不要关闭本页面！");
                        backup(data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                    
                        updateAlert(data.info,'alert-error');
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        
                        setTimeout(function(){
                            $('#top-alert').find('button').click();
                            $(that).removeClass('disabled').prop('disabled',false);
                        },1500);
                    }
                },
                "json"
            );
            return false;
        });

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.status){
                    showmsg(tab.id, data.info);
                    if(!$.isPlainObject(data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.tab, tab.id != data.tab.id);
                } else {
                    updateAlert(data.info,'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled',false);
                    },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
    </script>
    <include file="Public:footer" />
    </body>
</html>
